package com.oot2d.entity;

import com.oot2d.event.EventManager;
import com.oot2d.util.Epsilon;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Vector2f;

public class NPC extends Entity {
    protected float maxHealth;
    protected float health;
    protected boolean attackable;

    protected boolean moving;
    protected long steps;
    protected Vector2f dest;

    protected EventManager events;

    private boolean test = false;

    public NPC(String s, int w, int h, boolean att, float hp)
    {
        super(s, w, h);

        maxHealth = hp;
        health = hp;
        attackable = att;

        events = new EventManager();
    }

    public NPC(String s, int w, int h, boolean att, float hp, int a)
    {
        super(s, w, h, a);

        maxHealth = hp;
        health = hp;
        attackable = att;

        events = new EventManager();
    }

    public NPC(String s, int w, int h, boolean att, float hp, float bw, float bh, float xoff, float yoff)
    {
        super(s, w, h, bw, bh, xoff, yoff);

        maxHealth = hp;
        health = hp;
        attackable = att;

        events = new EventManager();
    }

    public NPC(String s, int w, int h, boolean att, float hp, float bw, float bh, float xoff, float yoff, int a)
    {
        super(s, w, h, bw, bh, xoff, yoff, a);

        maxHealth = hp;
        health = hp;
        attackable = att;

        events = new EventManager();
    }

    public NPC(NPC n)
    {
        super(n);

        maxHealth = n.maxHealth;
        health = n.health;
        attackable = n.attackable;

        events = new EventManager();
    }

    public float getHealth()
    {
        return health;
    }

    public float getMaxHealth()
    {
        return maxHealth;
    }

    public boolean isAttackable()
    {
        return attackable;
    }

    public EventManager getEvents()
    {
        return events;
    }

    public boolean isMoving()
    {
        return moving;
    }

    public void moveTo(float px, float py, float v)
    {
        dest = new Vector2f(px, py);
        steps = (long)(Math.sqrt((dest.x - x) * (dest.x - x) + (dest.y - y) * (dest.y - y)) / v);

        float dx = px - x;
        float dy = py - y;
        float a = (float)Math.atan2(dy, dx);
        vel.x = (float)(v * Math.cos(a));
        vel.y = (float)(v * Math.sin(a));

        moving = true;
    }

    public void update(int d)
    {
        events.checkEvents();

        //Primitive AI
        if (events.empty())
            EventManager.runScript(id, "ai", "wait_move");

        if (moving) {
            steps -= d;
            if (steps <= 0) {
                steps = 0;
                moving = false;
            }

            if (aabb != null)
                aabb.update(d, false);

            if (Epsilon.equals(vel.x, 0) && Epsilon.equals(vel.y, 0)) {
                steps = 0;
                moving = false;
                return;
            }

            x = (dest.x - (steps * vel.x));
            y = (dest.y - (steps * vel.y));

        }

        if (vel.x < -Epsilon.epsilon)
            dir = Direction.Left;

        if (vel.x > Epsilon.epsilon)
            dir = Direction.Right;

        if (vel.y < -Epsilon.epsilon)
            dir = Direction.Up;

        if (vel.y > Epsilon.epsilon)
            dir = Direction.Down;

        currAnim = anims[dir.val()];

    }

    public void draw(Graphics g)
    {
        if(moving) {
            if(currAnim != null)
                currAnim.draw(x, y);
        } else {
            sheet.startUse();
            sheet.getSubImage(dir.val(), 0).drawEmbedded(x, y, width, height);
            sheet.endUse();
            //g.drawString(((Integer)id).toString(), x, y - 20);
        }
    }
}
